
import re

CURRENT_SETTINGS = {
    'DIR_INPUT_PATH' : "",
    'DIR_FORMAT_PATH' : "",
    'DIR_LOG_PATH' : "",
    'DELIMITER' : ' ',
    'LEVEL_MARKER' : '.',
    'PREFIX' : '\\t',
    'PRE_LEVEL' : '2',
    'PRE_TITLE' : "小结;习题;本章小结",
    "PDF_INPUT_PATH" : 'testpdf.pdf',
    "PDF_OUTPUT_PATH" : '',
    "OFFSET" : "0"
}

DEFAULT_SETTINGS = {
    'DIR_INPUT_PATH' : "",
    'DIR_FORMAT_PATH' : "",
    'DIR_LOG_PATH' : "",
    'DELIMITER' : ' ',
    'LEVEL_MARKER' : '.',
    'PREFIX' : '\\t',
    'PRE_LEVEL' : '2',
    'PRE_TITLE' : "小结;习题;本章小结",
    "PDF_INPUT_PATH" : 'testpdf.pdf',
    "PDF_OUTPUT_PATH" : '',
    "OFFSET" : "0"
}

IS_PRINT_ACTICE_ONLY = 1
IS_FORMAT_DIR_FIRST = 1
IS_PRINT_PROCESS = 1

# 是否格式化
IS_FORMATED = False

def parse_setting(setting_path):
    """解析配置文件

    Args:
        setting_path (str): 配置文件路径

    Returns:
        dic: 配置参数的字典
    """

    param_dic = {}
    re_pstr = r'''
        (\S+)       # 属性项
        (?:\s+)     # 跳过空格
        =           # 匹配 = 
        (?:\s+)     # 跳过空格
        (?:[\"\']?) # 跳过引号
        ([^\"\'\n]*)  # 保留非引号的内容
        (?:[\"\']?) # 跳过引号
    '''
    pattern = re.compile(re_pstr, re.VERBOSE)

    with open(setting_path, mode='r', encoding='utf-8') as fin:
        line = fin.readline()
        while len(line):
            if line[0] != '#' and line[0] != '\n':
                res = pattern.findall(line)
                param_dic[res[0][0]] = res[0][1]
            line = fin.readline()

    return param_dic

